package com.woniu.utils.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;

import lombok.Data;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@ConfigurationProperties(prefix = "huyi.sms")
@Configuration
@Component
@Data
public class SMSUtil {
    @Autowired
    private RedisUtil redisUtil;
    private String api;
    private String account;
    private String password;
    public String sendCode(String mobile,String code){
          String content="您的验证码是："+code+"。请不要把验证码泄露给其他人。";

        //创建httpClient 对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(api);
        Map<String, Object> params = new HashMap<String,Object>();
        params.put("account", account);
        params.put("password", password);
        params.put("mobile", mobile);
        params.put("content", content);
        params.put("format", "json");
        HttpResponse response = HttpRequest.post(api).form(params).execute();
        String result = JSONUtil.parseObj(response.body()).get("code").toString();
        return result;
    }
}
